package com.bicon.botu.finance.payment.service;

import com.bicon.botu.finance.payment.entity.PayInfoEntity;
import com.bicon.botu.finance.payment.repository.PayInfoRepository;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: 宋旭
 * @Date: Created in 11:10 2017/7/26/026
 */
@Service
@Log4j
public class PayInfoService {

    @Autowired
    private PayInfoRepository payInfoRepository;

    /**
     * 保存支付信息
     *
     * @param code     App CODE
     * @param orderNum 订单编号
     * @param money    订单金额
     * @param type     支付类型
     */
    public void savePayInfo(String code, String orderNum, String money, String type, String attr, Date payTime) {
        PayInfoEntity payInfoEntity = payInfoRepository.findOneByOrderNoAndCodeAndPayResult(orderNum, code, PayInfoEntity.PAY_RESULT_WAIT);
        if (payInfoEntity == null) {
            payInfoEntity = new PayInfoEntity();
            payInfoEntity.preInsert();
            payInfoEntity.setCode(code);
            payInfoEntity.setOrderNo(orderNum);
            payInfoEntity.setPayResult(PayInfoEntity.PAY_RESULT_WAIT);
            payInfoEntity.setAttr(attr);
            payInfoEntity.setPayTime(payTime);
        }
        payInfoEntity.setOrderMoney(money);
        payInfoEntity.setPayType(type);
        payInfoRepository.save(payInfoEntity);
    }

    /**
     * 根据订单号，appcode获得支付信息
     * @param orderNo
     * @param code
     * @return
     */
    public PayInfoEntity findOneByOrderNoAndCode(String orderNo, String code){
        return payInfoRepository.findOneByOrderNoAndCode(orderNo, code);
    }

    /**
     * 根据订单号，appcode,结果获得支付信息
     * @param orderNo
     * @param code
     * @param result
     * @return
     */
    public PayInfoEntity findOneByOrderNoAndCodeAndPayResult(String orderNo, String code, String result){
        return payInfoRepository.findOneByOrderNoAndCodeAndPayResult(orderNo, code, result);
    }

    /**
     *
     * @param payNo
     * @param payMoney
     * @param result
     */
    public void saveByPayNoAndMoneyAndResult(String payNo, String payMoney, String result){
        PayInfoEntity payInfoEntity = new PayInfoEntity();
        payInfoEntity.setPayNo(payNo);
        payInfoEntity.setPayMoney(payMoney);
        payInfoEntity.setPayResult(result);
        payInfoRepository.save(payInfoEntity);
    }


}
